package com.css.dzj.dzjjcss.modules.statistics.controller;

import com.alibaba.fastjson.JSON;
import com.css.dzj.dzjjcss.common.audit.service.SysAuditService;
import com.css.dzj.dzjjcss.common.util.Constant;
import com.css.dzj.dzjjcss.common.util.R;
import com.css.dzj.dzjjcss.modules.press.entity.Press;
import com.css.dzj.dzjjcss.modules.resultregister.entity.ResultRegisterEntity;
import com.css.dzj.dzjjcss.modules.statistics.service.StatisticsService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 查询统计
 *
 * @author Leon
 */
@RequestMapping("/statistics")
@RestController
public class StatisticsController {
    @Autowired
    private StatisticsService statisticsService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private SysAuditService sysAuditService;

    /**
     * 分页查询
     *
     * @param projectName
     * @param createUser
     * @param createUserUnit
     * @param status         状态
     * @param startTime      创建时间-开始
     * @param endTime        创建时间-结束
     * @param  administrativeDivision
     * @param supervisionType
     * @param  unitName
     * @param pageSize
     * @param pageNo
     * @return
     */
    @GetMapping("/getPage")
    public R getPage(@RequestParam String projectName,
                     @RequestParam String createUser,
                     @RequestParam String createUserUnit,
                     @RequestParam String status,
                     @RequestParam String startTime,
                     @RequestParam String endTime,
                     @RequestParam String administrativeDivision,
                     @RequestParam String supervisionType,
                     @RequestParam String unitName,
                     @RequestParam Integer pageSize, @RequestParam Integer pageNo) {
        String s="";
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<ResultRegisterEntity> page = statisticsService.getPage(projectName, createUser, createUserUnit,
                    status,startTime, endTime, administrativeDivision, supervisionType,unitName, pageRequest);
            s = JSON.toJSONString(page);
            sysAuditService.saveSuccessAudit("分页查询成功", Constant.CXTJ, Constant.PT,Constant.PAGE,s,request);
            return R.ok().put("data", page);
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("分页查询失败",Constant.CXTJ,Constant.PT,Constant.PAGE,s,e,request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 导出
     **/
    @GetMapping("/export")
    public R export(HttpServletResponse response,
                    @RequestParam String projectName,
                    @RequestParam String createUser,
                    @RequestParam String createUserUnit,
                    @RequestParam String status,
                    @RequestParam String startTime,
                    @RequestParam String endTime,
                    @RequestParam String administrativeDivision,
                    @RequestParam String supervisionType,
                    @RequestParam String unitName ) {
        String s="";
        try {
            PageRequest pageRequest = PageRequest.of(0, 200);
            QueryResults<ResultRegisterEntity> page = statisticsService.getPage(projectName, createUser, createUserUnit,
                    status,startTime, endTime, administrativeDivision, supervisionType,unitName, pageRequest);
            List<ResultRegisterEntity> registerEntityList = page.getResults();
            statisticsService.exportToExcel(response, registerEntityList);
            s = JSON.toJSONString(registerEntityList);
            sysAuditService.saveSuccessAudit("导出成功", Constant.CXTJ, Constant.PT,Constant.EXPORT,s,request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("导出失败",Constant.CXTJ,Constant.PT,Constant.EXPORT,s,e,request);
            return R.error(e.getMessage());
        }
    }


    /**
     * 统计图
     *
     * @return
     */
    @GetMapping("/statistical")
    public R statistical() {
        try {
            List<Object[]> chartList = statisticsService.chartList();
            ArrayList<Object> data = new ArrayList<>();
            ArrayList<Object> x = new ArrayList<>();
            ArrayList<Object> y = new ArrayList<>();
            for (Object[] o : chartList){
                x.add(o[0]);
                y.add(o[1]);
            }
            data.add(x);
            data.add(y);
            return R.ok().put("data", data);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    /**
     * 催办
     *
     * @param press
     * @return
     */
    @PostMapping("/urge")
    public R urge(@RequestBody Press press) {
        String s="";
        try {
            statisticsService.urge(press);
            s = JSON.toJSONString(press);
            sysAuditService.saveSuccessAudit("催办成功", Constant.CXTJ, Constant.PT,Constant.CB,s,request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("催办失败",Constant.CXTJ,Constant.PT,Constant.CB,s,e,request);
            return R.error(e.getMessage());
        }
    }
}
